package com.yuyou.fn.platform.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.yuyou.fn.platform.mapper.SysRoleResourceMapper;
import com.yuyou.fn.platform.entity.SysRoleResource;
import com.yuyou.fn.platform.service.ISysRoleResourceService;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * SysRoleResource 表数据服务层接口实现类
 *
 */
@Service
public class SysRoleResourceServiceImpl extends SuperServiceImpl<SysRoleResourceMapper, SysRoleResource,Long> implements ISysRoleResourceService {

    @Override
    public List<SysRoleResource> findRoleResourceByRoleId(Long roleId) {

        Example<SysRoleResource> example = SysRoleResource.createExample();
        example.createCriteria().andEqualTo("roleId",roleId) ;
        return findListByExample(example);
    }

    @Override
    public List<SysRoleResource> findRoleResourceByRoleIds(List<Long> roleIds) {
        Example<SysRoleResource> example = SysRoleResource.createExample();
        example.createCriteria().andIn("roleId",roleIds);
        return findListByExample(example);
    }

    @CacheEvict(value = "CacheOfResource",allEntries = true )
    @Transactional
    @Override
    public void saveOrUpdateRoleResource(List<Long> resourceIds, Long roleId) {

        Example<SysRoleResource> example = SysRoleResource.createExample();
        example.createCriteria().andEqualTo("roleId" , roleId );

        deleteByExample(example);

        List<SysRoleResource> sysRoleResourceList = new ArrayList<>(resourceIds.size());

        for(Long resourceId : resourceIds ){
            SysRoleResource sysRoleResource = new SysRoleResource();
            sysRoleResource.setRoleId(roleId);
            sysRoleResource.setResourceId(resourceId);
            sysRoleResourceList.add(sysRoleResource);
        }
        insertBatch(sysRoleResourceList);
    }

    @CacheEvict(value = "CacheOfResource",allEntries = true )
    @Override
    public void deleteRoleResource(Long roleId) {
        Example<SysRoleResource> example = SysRoleResource.createExample();
        example.createCriteria().andEqualTo("roleId" , roleId );
        deleteByExample(example);
    }

}